<?php

namespace App\Imports;

use App\Models\Assess\EvaluationMonitorConnectModel;
use App\Models\Assess\EvaluationMonitoringModel;
use App\Models\Assess\OrderAndCustomersOrderDetailModel;
use App\Models\Assess\OrderAndCustomersOrderExtendModel;
use App\Models\Assess\OrderAndCustomersOrderModel;
use App\Services\Common\AmazonComment;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;

class AmazonCommentImport implements ToCollection
{

    public $message = [];
    public $asin = '';
    public $month_abbr_list = [
        'January'=>1,
        'February'=>2,
        'March'=>3,
        'April'=>4,
        'May'=>5,
        'June'=>6,
        'July'=>7,
        'August'=>8,
        'September'=>9,
        'October'=>10,
        'November'=>11,
        'December'=>12,
    ];

    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {
        // 循环遍历数据
        $AmazonComment = new AmazonComment();
        $EvaluationMonitoringModel = new EvaluationMonitoringModel();
        $EvaluationMonitorConnectModel = new EvaluationMonitorConnectModel();
        $filedChina = $AmazonComment->filedChina;
        $fieldListName = [];

        if (empty($this->asin)) $collection = [];

        foreach ($collection as $row) {
            try {
                $itemRow = $row->toArray();
                $itemFirst = $itemRow[0] ?? [];
                if (in_array($itemFirst,$filedChina)){
                    $fieldListName = $itemRow;
                    continue;
                }

                if (empty($itemRow[0])) continue;
                $item = [];
                foreach ($itemRow as $k1=>$v1){
                    $item[$fieldListName[$k1]] = $v1;
                }

                    //添加评价监控信息
                    $monitorData = $EvaluationMonitoringModel->getOne([
                        ['asin', '=', $this->asin],
                        ['mark', '=', 1],
                    ]);
                    if (empty($monitorData)) continue;

                    $monitorId = $monitorData['id'];

                    $fieldList = [];
                    foreach ($filedChina as $fieldK=>$fieldV){
                        $fieldList[$fieldK] = $item[$fieldV] ?? '';
                        if ($fieldK == 'stars') $fieldList[$fieldK] = intval($fieldList[$fieldK]);
                    }

                    // 空数据进行处理
                    if (empty($fieldList['evaluation_user'])) continue;


                    if (strpos($fieldList['look'],'Reviewed in') !==false){
                        $lookExp = explode('on ',$fieldList['look']);
                    }else{
                        $lookExp = explode(' ',$fieldList['look']);
                    }

                    if (count($lookExp)>=3 && strpos($fieldList['look'],'年') !==false){
                        $commentTime = $lookExp[1] ?? '';
                        $commentTime = str_replace(['年','月'], '-', $commentTime);
                        $commentTime = str_replace(['日'], '', $commentTime);
                        $fieldList['evaluation_time'] = $commentTime;
                    }elseif(strpos($fieldList['look'],'年') !==false){
                        $commentTime = $lookExp[0] ?? '';
                        $commentTime = str_replace(['年','月'], '-', $commentTime);
                        $commentTime = str_replace(['日'], '', $commentTime);
                        $fieldList['evaluation_time'] = $commentTime;
                    }elseif(strpos($fieldList['look'],'Reviewed in') !==false){
                        $commentTime = $lookExp[1] ?? '';
                        $fieldList['evaluation_time'] = $commentTime;
                    }

                    if (strpos($fieldList['evaluation_time'],'年') ===false){
                        $lookExp = explode(' on ',$fieldList['look']);
                        if (count($lookExp)>=2){
                            $commentTime = $lookExp[1] ?? '';
                            $commentTime = str_replace([', '], ' ', $commentTime);
                            $commentTimeExp = explode(' ',$commentTime);
                            $month = $commentTimeExp[0] ?? '';
                            $day = $commentTimeExp[1] ?? '';
                            $year = $commentTimeExp[2] ?? '';
                            $fieldList['evaluation_time'] = $year.'-'.($this->month_abbr_list[$month] ?? '').'-'.$day;
                        }
                    }else{
                        $commentTime = $lookExp[0] ?? '';
                        $commentTime = str_replace(['年','月'], '-', $commentTime);
                        $commentTime = str_replace(['日'], '', $commentTime);
                        $fieldList['evaluation_time'] = $commentTime;
                    }

                    if (isset($fieldList['look'])) unset($fieldList['look']);
                    //判断是否存在
                    $monitorConnectExists = $EvaluationMonitorConnectModel->getOne([
                        ['evaluation_monitor_id', '=', $monitorId],
                        ['evaluation_title', '=', $fieldList['evaluation_title']],
                        ['evaluation_user', '=', $fieldList['evaluation_user']],
                        ['evaluation_time', '=', $fieldList['evaluation_time']],
                        ['mark', '=', 1],
                    ]);

                    if (empty($monitorConnectExists)){
                        $fieldList['create_time'] = time();
                        $fieldList['evaluation_monitor_id'] = $monitorId;
                        $EvaluationMonitorConnectModel->insert($fieldList);
                    }

            } catch (\Exception $e) {
                $logInfo = $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine();
                $this->message[] = $logInfo;
                Log::channel('bazhuayuLog')->debug('============导入amazon评论异常================');
                Log::channel('bazhuayuLog')->debug($logInfo);
            }
        }
    }

}



